#include "soundLoader.h"

FMOD::System* sound::s_system = NULL;

void ERRCHECK(FMOD_RESULT result)
{
	 if (result != FMOD_OK)
	 {
		  std::cout << "FMOD error " << result << FMOD_ErrorString(result);
		  //printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
//			exit(-1);
	 }
}

sound::sound()
{
	offset = 0;
	channel = NULL;
	soundid = NULL;
	bpmpitch = NULL;
}

sound::~sound()
{
	FMOD_RESULT result;
	result = soundid->release();
	ERRCHECK(result);
}

void sound::shutdown()
{
	cout << "shutting down FMOD " << endl;
	FMOD_RESULT result;
	result = s_system->close();
	ERRCHECK(result);
	result = s_system->release();
	ERRCHECK(result);
}

void sound::init()
{
	unsigned int version;
	FMOD_RESULT result;

	result = FMOD::System_Create(&s_system);
	ERRCHECK(result);
	result = s_system->getVersion(&version);
	ERRCHECK(result);

	if (version < FMOD_VERSION)
	{
		 std::cout << "Error!  You are using an old version of FMOD %08x.  This program requires %08x\n" << version << FMOD_VERSION << std::endl;
		//printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION);
//		 getch();
	}

//	 ERRCHECK(result);
//	 result = s_system->setOutput(FMOD_OUTPUTTYPE_ALSA);
//	 ERRCHECK(result);
	
	ERRCHECK(result);
	result = s_system->init(32, FMOD_INIT_NORMAL, 0);
	ERRCHECK(result);

}

bool sound::load(const char *filename)
{
	if (sound::s_system == NULL) sound::init();

	FMOD_RESULT result;
	result = s_system->createSound(filename, FMOD_SOFTWARE | FMOD_2D  , 0, &soundid);
	ERRCHECK(result);
	result = soundid->setMode(FMOD_LOOP_OFF);
	ERRCHECK(result);
	path = filename ;

	if(result!=FMOD_OK) return false;
	else
	{
		cout << "loaded sound " << filename << endl;
		return true;
	}
}

bool sound::loadstream(const char *filename)
{
	if (sound::s_system == NULL) sound::init();

	FMOD_RESULT result;
	result = s_system->createSound(filename, FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM , 0, &soundid);
	ERRCHECK(result);
	result = soundid->setMode(FMOD_LOOP_OFF);
	ERRCHECK(result);
	path =  filename ;

	if(result!=FMOD_OK) return false;
	else
	{
		cout << "loaded stream " << filename << endl;
		return true;
	}

}

void sound::setPitch(float f)
{
	FMOD_RESULT result;
	if(bpmpitch==NULL)
	{
		result = s_system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&bpmpitch);
		ERRCHECK(result);

		result = s_system->addDSP(bpmpitch, &_pitchConnection);
		ERRCHECK(result);
	}
//	 if(f==1.0) bpmpitch->remove();
	result = bpmpitch->setParameter(0,f);
	ERRCHECK(result);
}

void sound::play()
{
	FMOD_RESULT result;
	result = s_system->playSound(FMOD_CHANNEL_FREE, soundid, 0, &channel);
	ERRCHECK(result);
	s_system->update();
	channel->getFrequency(&frequency);
}

int sound::getPosition()
{
	unsigned int i ;
	channel->getPosition(&i,FMOD_TIMEUNIT_MS) ;
	return i+offset;
}

void sound::setFrequency(float newfreq)
{
	channel->setFrequency(newfreq);frequency=newfreq;
}

void sound::addOffset(int increment_ms)
{
	offset += increment_ms;
}

void sound::setPosition(unsigned int ms)
{
	channel->setPosition(ms,FMOD_TIMEUNIT_MS);
}

float sound::getLength()
{
	unsigned int i; 
	soundid->getLength(&i,FMOD_TIMEUNIT_MS);
	return i/1000.0;
}
